package com.secsys.service.impl;
import java.util.ArrayList;
import java.util.List;

import com.secsys.dto.ResourceDTO;
import com.secsys.model.TbBbsModule;
import com.secsys.model.TbSysResource;
import com.secsys.service.ResourceService;
/**
 * @Description 资源业务逻辑接口-实现类
 * @author LiusQ
 *
 */
public class ResourceServiceImpl extends ServiceImpl<TbSysResource, Integer> implements
		ResourceService {
	
	private BbsModuleServiceImpl bbsModuleService;
	
    /**
     * @Description 添加资源
     */
	@Override
	public void addResource(TbSysResource tbSysResource) {
		// TODO Auto-generated method stub
		this.dao.save(tbSysResource);
	}

	@Override
	public TbSysResource getBeanFromDto(ResourceDTO resourceDto) {
		// TODO Auto-generated method stub
	    	TbSysResource tbSysResource = new TbSysResource();
	    	tbSysResource.setId(resourceDto.getId());
	    	tbSysResource.setMenuName(resourceDto.getMenuName());
	    	tbSysResource.setSequenceNum(resourceDto.getSequenceNum());
	    	tbSysResource.setUri(resourceDto.getUri());
	    	tbSysResource.setParentFlag(resourceDto.getParentFlag());
	    	return tbSysResource;
	}

	public BbsModuleServiceImpl getBbsModuleService() {
		return bbsModuleService;
	}

	public void setBbsModuleService(BbsModuleServiceImpl bbsModuleService) {
		this.bbsModuleService = bbsModuleService;
	}

	/**
	 * 查找所有资源
	 */
	@Override
	public List<ResourceDTO> listResource() {
		// TODO Auto-generated method stub
		List<ResourceDTO> resourceList = getDtoFromList(this.dao.find("from TbSysResource"));
		
		return resourceList;
	}

	@Override
	public List<ResourceDTO> getDtoFromList(List<TbSysResource> tbResourceList) {
		// TODO Auto-generated method stub
		List<ResourceDTO> resourceList = new ArrayList<ResourceDTO>();
		for(TbSysResource tbResource : tbResourceList){
			resourceList.add(getDtoFromPojo(tbResource));
		}
		return resourceList;
	}

	@Override
	public ResourceDTO getDtoFromPojo(TbSysResource tbResource) {
		// TODO Auto-generated method stub
		ResourceDTO resource = new ResourceDTO();
		resource.setId(tbResource.getId());
		resource.setMenuName(tbResource.getMenuName());
		if(tbResource.getTbBbsModule()!=null){
			resource.setModelId(tbResource.getTbBbsModule().getId());
		}
		resource.setParentFlag(tbResource.getParentFlag());
		resource.setSequenceNum(tbResource.getSequenceNum());
		resource.setUri(tbResource.getUri());
		return resource;
	}

	@Override
	public List<ResourceDTO> listNotInResource(List<Integer> resourceIdList) {
		// TODO Auto-generated method stub
		String idValue = resourceIdList.toString().substring(1, resourceIdList.toString().length()-1);
		System.out.println(idValue);
		List<ResourceDTO> resourceList = getDtoFromList(this.dao.find("from TbSysResource where id not in (" + idValue+")"));
		return resourceList;
	}

	/**
     * 根据资源名查找资源
     * @param menuName
     * @return
     */
	@Override
	public TbSysResource findTbSysResourceByMenuName(String menuName) {
		// TODO Auto-generated method stub
		List<TbSysResource> tbResourceList = this.dao.findByProperty("from TbSysResource where menuName=?", menuName);
		if(tbResourceList!=null && tbResourceList.size()>0){
			return tbResourceList.get(0);
		}else{
			return null;
		}
	}

	@Override
	public List<ResourceDTO> findFatherResource() {
		// TODO Auto-generated method stub
		List<ResourceDTO> resourceList = getDtoFromList(this.dao.find("from TbSysResource where parentFlag=1 order by id desc"));
		return resourceList;
	}

	@Override
	public List<TbSysResource> findChildByFather(String sequenceNum) {
		// TODO Auto-generated method stub
		List<TbSysResource> resourceList = this.dao.find("from TbSysResource where sequenceNum like '"+ sequenceNum +"%' and parentFlag=0");
		
		return resourceList;
	}

	@Override
	public TbSysResource findTbSysResourceByMenuName(String menuName, Integer id) {
		// TODO Auto-generated method stub
		List<TbSysResource> tbResourceList = this.dao.find("from TbSysResource where menuName=? and id!=?", new Object[]{menuName,id});
		if(tbResourceList!=null && tbResourceList.size()>0){
			return tbResourceList.get(0);
		}else{
			return null;
		}
	}
       
	 

}
